﻿/**************************************************************
 *
 * 唯一标识：f55155ec-24f9-41a1-95c8-e5e3db127af6
 * 命名空间：Sgr.AuditLogs.Queries
 * 创建时间：2023/8/16 17:34:13
 * 机器名称：DESKTOP-S0D075D
 * 创建者：antho
 * 电子邮箱：fengqinhua2016@163.com
 * 描述：
 *
 **************************************************************/

using Microsoft.EntityFrameworkCore;
using Sgr.Application.Queries;
using Sgr.Application.ViewModels;
using Sgr.AuditLogs.Model;
using Sgr.AuditLogs.ViewModels;
using Sgr.Exceptions;

namespace Sgr.AuditLogs.Queries
{
    public class LogOperateQueries : ILogOperateQueries
    {
        private readonly ISgrQueryContext _context;
        private readonly ICurrentUser _currentUser;

        public LogOperateQueries(ISgrQueryContext context, ICurrentUser currentUser)
        {
            _context = context;
            _currentUser = currentUser;
        }

        public Task<LogOperateModel?> GetAsync(long id, CancellationToken cancellationToken = default)
        {
            if (id <= 0)
                throw new BusinessException($"Id({id})需大于零");

            long orgId = QueryHelper.GetOrgId(_currentUser);

            var query = _context.Query<LogOperate>()
                 .Where(f => f.Id == id)
                 .Where(f => f.OrgId == orgId)
                 .Select(f => new LogOperateModel()
                 {
                     Id = f.Id,
                     ClientBrowser = f.ClientBrowser,
                     ClientOs = f.ClientOs,
                     HttpMethod = f.HttpMethod,
                     IpAddress = f.IpAddress,
                     Location = f.Location,
                     LoginName = f.LoginName,
                     OperateWay = f.OperateWay,
                     Remark = f.Remark,
                     RequestDescription = f.RequestDescription,
                     RequestDuration = f.RequestDuration,
                     RequestParam = f.RequestParam,
                     RequestTime = f.RequestTime,
                     RequestUrl = f.RequestUrl,
                     Status = f.Status,
                     DisplayName = f.DisplayName,
                     OrgId = f.OrgId
                 });

            return _context.FirstOrDefaultAsync(query, cancellationToken);
        }

        public async Task<PagedResponse<LogOperateListModel>> GetListAsync(LogOperateSearchModel request, CancellationToken cancellationToken = default)
        {
            Check.NotNull(request, nameof(request));

            long orgId = QueryHelper.GetOrgId(_currentUser);

            //设置查询条件
            var query = _context.Query<LogOperate>()
                .WithOrg(orgId);

            if (request.OnlyCurrentUser)
            {
                //long userId = QueryHelper.GetUserId(_currentUser);
                query = query.Where(f => f.LoginName == _currentUser.LoginName);
            }
            else
            {
                if (!string.IsNullOrEmpty(request.RequestUser))
                    query = query.Where(f => EF.Functions.Like(f.LoginName, $"{request.RequestUser}%"));
            }

            if (request.RequestStart != null)
                query = query.Where(f => f.RequestTime >= request.RequestStart!);

            if (request.RequestEnd != null)
                query = query.Where(f => f.RequestTime <= request.RequestEnd!);

            if (!string.IsNullOrEmpty(request.RequestDescription))
                query = query.Where(f => EF.Functions.Like(f.RequestDescription, $"%{request.RequestDescription}%"));

            if (request.Status != null)
                query = query.Where(f => f.Status == request.Status!);

            //设置排序方式
            //query = request.IsAscending ? query.OrderBy(f => f.Id) : query.OrderByDescending(f => f.Id);
            query = query.OrderByDescending(f => f.Id);

            //设置选择器
            var select = query.Select(f => new LogOperateListModel()
            {
                Id = f.Id,
                ClientBrowser = f.ClientBrowser,
                ClientOs = f.ClientOs,
                IpAddress = f.IpAddress,
                Location = f.Location,
                LoginName = f.LoginName,
                DisplayName = f.DisplayName,
                Remark = f.Remark,
                RequestDescription = f.RequestDescription,
                RequestDuration = f.RequestDuration,
                RequestTime = f.RequestTime,
                RequestUrl = f.RequestUrl,
                Status = f.Status,
                HttpMethod = f.HttpMethod,
                OrgId = f.OrgId
            });

            return await _context.PagedListByPageSizeAsync(select, request.PageIndex, request.PageSize, cancellationToken);
        }
    }
}